use utf8;
package iSOCK::Schema::App::Result::User;

# Created by DBIx::Class::Schema::Loader
# DO NOT MODIFY THE FIRST PART OF THIS FILE

=head1 NAME

iSOCK::Schema::App::Result::User

=cut

use strict;
use warnings;

use Moose;
use MooseX::NonMoose;
use MooseX::MarkAsMethods autoclean => 1;
extends 'DBIx::Class::Core';

=head1 COMPONENTS LOADED

=over 4

=item * L<DBIx::Class::InflateColumn::DateTime>

=back

=cut

__PACKAGE__->load_components("InflateColumn::DateTime");

=head1 TABLE: C<user>

=cut

__PACKAGE__->table("user");

=head1 ACCESSORS

=head2 id

  data_type: 'varchar'
  is_nullable: 0
  size: 256

=head2 name

  data_type: 'varchar'
  is_nullable: 1
  size: 256

=head2 mail

  data_type: 'varchar'
  is_nullable: 1
  size: 256

=head2 location

  data_type: 'varchar'
  is_nullable: 1
  size: 256

=head2 password

  data_type: 'varchar'
  is_nullable: 1
  size: 256

=head2 session_id

  data_type: 'varchar'
  is_nullable: 1
  size: 256

=head2 last_login

  data_type: 'timestamp'
  is_nullable: 1

=head2 fake

  data_type: 'boolean'
  is_nullable: 1

=head2 rest_hash

  data_type: 'char'
  is_nullable: 1
  size: 40

=head2 subversion_id

  data_type: 'varchar'
  is_nullable: 1
  size: 256

=cut

__PACKAGE__->add_columns(
  "id",
  { data_type => "varchar", is_nullable => 0, size => 256 },
  "name",
  { data_type => "varchar", is_nullable => 1, size => 256 },
  "mail",
  { data_type => "varchar", is_nullable => 1, size => 256 },
  "location",
  { data_type => "varchar", is_nullable => 1, size => 256 },
  "password",
  { data_type => "varchar", is_nullable => 1, size => 256 },
  "session_id",
  { data_type => "varchar", is_nullable => 1, size => 256 },
  "last_login",
  { data_type => "timestamp", is_nullable => 1 },
  "fake",
  { data_type => "boolean", is_nullable => 1 },
  "rest_hash",
  { data_type => "char", is_nullable => 1, size => 40 },
  "subversion_id",
  { data_type => "varchar", is_nullable => 1, size => 256 },
);

=head1 PRIMARY KEY

=over 4

=item * L</id>

=back

=cut

__PACKAGE__->set_primary_key("id");

=head1 UNIQUE CONSTRAINTS

=head2 C<user_name_unique>

=over 4

=item * L</name>

=back

=cut

__PACKAGE__->add_unique_constraint("user_name_unique", ["name"]);

=head2 C<user_rest_hash_unique>

=over 4

=item * L</rest_hash>

=back

=cut

__PACKAGE__->add_unique_constraint("user_rest_hash_unique", ["rest_hash"]);

=head2 C<user_subversion_id_unique>

=over 4

=item * L</subversion_id>

=back

=cut

__PACKAGE__->add_unique_constraint("user_subversion_id_unique", ["subversion_id"]);

=head1 RELATIONS

=head2 sessions

Type: has_many

Related object: L<iSOCK::Schema::App::Result::Session>

=cut

__PACKAGE__->has_many(
  "sessions",
  "iSOCK::Schema::App::Result::Session",
  { "foreign.user_id" => "self.id" },
  { cascade_copy => 0, cascade_delete => 0 },
);

=head2 user_to_datasheets

Type: has_many

Related object: L<iSOCK::Schema::App::Result::UserToDatasheet>

=cut

__PACKAGE__->has_many(
  "user_to_datasheets",
  "iSOCK::Schema::App::Result::UserToDatasheet",
  { "foreign.user_id" => "self.id" },
  { cascade_copy => 0, cascade_delete => 0 },
);

=head2 user_to_preferences

Type: has_many

Related object: L<iSOCK::Schema::App::Result::UserToPreference>

=cut

__PACKAGE__->has_many(
  "user_to_preferences",
  "iSOCK::Schema::App::Result::UserToPreference",
  { "foreign.user_id" => "self.id" },
  { cascade_copy => 0, cascade_delete => 0 },
);

=head2 user_to_products

Type: has_many

Related object: L<iSOCK::Schema::App::Result::UserToProduct>

=cut

__PACKAGE__->has_many(
  "user_to_products",
  "iSOCK::Schema::App::Result::UserToProduct",
  { "foreign.user_id" => "self.id" },
  { cascade_copy => 0, cascade_delete => 0 },
);

=head2 user_to_roles

Type: has_many

Related object: L<iSOCK::Schema::App::Result::UserToRole>

=cut

__PACKAGE__->has_many(
  "user_to_roles",
  "iSOCK::Schema::App::Result::UserToRole",
  { "foreign.user_id" => "self.id" },
  { cascade_copy => 0, cascade_delete => 0 },
);

=head2 user_to_specifications

Type: has_many

Related object: L<iSOCK::Schema::App::Result::UserToSpecification>

=cut

__PACKAGE__->has_many(
  "user_to_specifications",
  "iSOCK::Schema::App::Result::UserToSpecification",
  { "foreign.user_id" => "self.id" },
  { cascade_copy => 0, cascade_delete => 0 },
);

=head2 roles

Type: many_to_many

Composing rels: L</user_to_roles> -> role

=cut

__PACKAGE__->many_to_many("roles", "user_to_roles", "role");


# Created by DBIx::Class::Schema::Loader v0.07033 @ 2012-09-12 16:11:25
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:be2s7Ezz39xbtqw+lIqChw

__PACKAGE__->many_to_many ( qw( preferences user_to_preferences preference ) );

sub assert_user_role {
  my ( $self, $role ) = @_;
  
  return 1 if grep { $_->id eq $role } $self->roles;
  return 0;
}

sub has_preference {
  my ( $self, $preference ) = @_;
  
  return 1 if grep { $_->id eq $preference } $self->preferences;
  return 0;
}

sub get_preference_value {
  my ( $self, $preference ) = @_;
  
  my ( $value ) = map { $_->value } grep { $_->preference_id eq $preference } $self->user_to_preferences;
  
  return $value;
}

sub datasheet_sources {
  my ( $self ) = @_;
  
  for ( grep { defined } $self->get_preference_value ( 'datasheet_source' ) ) {
    return ( 'Subversion',         'Subversion (tags)',  'Subversion (trunk)' ) if $_ eq 'SVN';
    return ( 'Subversion (tags)',  'Subversion (trunk)', 'Subversion'         ) if $_ eq 'SVN/tags';
    return ( 'Subversion (trunk)', 'Subversion',         'Subversion (tags)'  ) if $_ eq 'SVN/trunk';
  }

  return ( 'Subversion', 'Subversion (tags)', 'Subversion (trunk)' );
}

sub authorizations_h {
  my ( $self ) = @_;
  
  my ( $product, $specification, $datasheet );

  push @{ $product       ->{ $_->product_id       } }, $_->authorization_id for $self->user_to_products;
  push @{ $specification ->{ $_->specification_id } }, $_->authorization_id for $self->user_to_specifications;
  push @{ $datasheet     ->{ $_->datasheet_id     } }, $_->authorization_id for $self->user_to_datasheets;

  my $h;

  my $role;
  $role->{$_} = 1 for grep { $self->assert_user_role ( $_ ) } map { $_->id } $self->roles;

  $h = {
    id             => $self->id,
    name           => $self->name,
    products       => [ map { { id => $_, map { ( $_ => 1 ) } @{ $product       ->{ $_ } } } } sort keys %$product       ],
    specifications => [ map { { id => $_, map { ( $_ => 1 ) } @{ $specification ->{ $_ } } } } sort keys %$specification ],
    datasheets     => [ map { { id => $_, map { ( $_ => 1 ) } @{ $datasheet     ->{ $_ } } } } sort keys %$datasheet     ],
    role           => $role,
  };
  
  $h->{fake}     = 1               if $self->fake;
  $h->{location} = $self->location if $self->location;
  $h->{mail}     = $self->mail     if $self->mail;
  
  return $h;
}

sub mail_address {
  my ( $self ) = @_;
  
  return '"' . $self->name . '" <' . $self->mail . '>'
}

sub login_uri {
  my ( $self ) = @_;
  
  my @uris = map { $_->value } grep { $_->preference_id eq 'login_uri' } $self->user_to_preferences;
  
  return @uris ? $uris[0] : undef;
}

sub bofh {
  my ( $self ) = @_;
  
  $self->assert_user_role ( 'can_bofh' );
}

sub can_alter_all_datasheets {
  my ( $self ) = @_;
  
  return $self->assert_user_role ( 'owns_all_datasheets' ) || $self->assert_user_role ( 'writes_all_datasheets' );
}

sub can_alter_datasheet {
  my ( $self, $datasheet_id ) = @_;
  
  return 1 if $self->can_alter_all_datasheets;
  
  return ( scalar grep { $_->datasheet_id eq $datasheet_id } $self->user_to_datasheets ) ? 1 : 0;
}

sub can_alter_all_specifications {
  my ( $self ) = @_;
  
  return $self->assert_user_role ( 'owns_all_specifications' ) || $self->assert_user_role ( 'writes_all_specifications' );
}

sub can_alter_specification {
  my ( $self, $specification_id ) = @_;
  
  return 1 if $self->can_alter_all_specifications;
  
  return ( scalar grep { $_->specification_id eq $specification_id } $self->user_to_specifications ) ? 1 : 0;
}

sub can_alter {
  my ( $self, $content_id, $whatever_id ) = @_;
  
  return 1 if $content_id eq 'specification' && $self->can_alter_specification ( $whatever_id );
  return 1 if $content_id eq 'datasheet'     && $self->can_alter_datasheet     ( $whatever_id );
  
  return;
}

# You can replace this text with custom code or comments, and it will be preserved on regeneration
__PACKAGE__->meta->make_immutable;
1;
